`
https://leetcode.cn/problems/minimum-size-subarray-in-infinite-array/
`

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var minSizeSubarray = function (nums, target) {
  const n = nums.length
  let sum = 0
  let res = Infinity
  let left = 0, right = 0
  const numsSum = nums.reduce((acc, cur) => acc + cur)
  // 要至少 k 个 nums 才能到达 target
  const k = Math.floor(target / numsSum)
  target -= k * numsSum

  // k 个 nums 刚好就是 target
  if (target === 0) return k * n

  // 剩下的部分只需要两个 nums 循环就能算出来
  while (right < n * 2 - 1) {
    const n1 = nums[right % n]
    sum += n1
    right++

    while (sum > target) {
      const n2 = nums[left % n]
      sum -= n2
      left++
    }

    if (sum === target) res = Math.min(res, right - left)
  }

  return res === Infinity ? -1 : res + k * n
};